<?php

class Messages extends Controller {

    function add() {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        if (isset($_POST["submit"])) {
            $message["subject"] = $_POST["subject"];
            $message["author_user_id"] = $_POST["author_user_id"];
            $message["content"] = $_POST["message_content"];

            if (isset($_SESSION['user_list'])) {
                $this->Message->add($message);
                $this->session->remove('user_list');
                $user = $this->session->get("user_id");
                $this->redirect("/messages/show/$user");
            } else {
                $this->_auto_render = false;
                $this->session->set_flash("No recipient choosed");
                $this->redirect("/messages/add");
            }
        }

        $this->set("title", "Write New Message");
    }

    function add_replay($user_id, $conversation_id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        if (isset($_POST["submit"])) {
            $message["content"] = $_POST["content_message_replay"];
            $message["conversation_id"] = $conversation_id;
            $message["author_user_id"] = $this->session->get("user_id");
            $this->Message->add_replay($message);
        }

        $this->_auto_render = false;
        $this->session->set_flash("Message reply sent");
        $this->redirect("/messages/view/$user_id/$conversation_id");
    }

    function add_recipient() {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }
        $recipients = $this->Message->add_recipient();
        $this->_render_header = false;
        $this->set("title", "Select Recipient");
        $this->set("recipients", $recipients);
    }

    function delete_replay($conversation_id, $message_id, $user_id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }
        $this->Message->delete_replay($conversation_id, $message_id);
        $this->_auto_render = false;
        $this->session->set_flash("Message deleted");
        $this->redirect("/messages/view/$user_id/$conversation_id");
    }

    function delete($conversation_id, $user_id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }
        $this->Message->delete($conversation_id, $user_id);
        $this->_auto_render = false;
        $this->session->set_flash("Message deleted");
        $this->redirect("/messages/show/$user_id");
    }

    function show($id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        if (isset($_SESSION['user_list'])) {
            $this->session->remove('user_list');
        }

        $messages = $this->Message->show($id);
        $this->set("title", "Message List");
        $this->set("messages", $messages);
    }

    function show_ajax($id, $page = "1") {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $offset = ((int)$page - 1) * 3;

        $messages = $this->Message->show_ajax($id, $offset);
        $this->_render_header = false;
        $this->set("messages", $messages);
    }

    function view($user_id, $id) {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }
        $messages = $this->Message->view($user_id, $id);
        $user_photo = $this->Message->get_user_photos($this->session->get("user_id"));
        $this->set("title", "View Message");
        $this->set("messages", $messages);
        $this->set("photo", $user_photo);
    }

    function view_ajax($id, $page = "1") {
        if (!$this->session->get("authenticated")) {
            $this->_auto_render = false;
            $this->session->set_flash("You must login first to perform that action.");
            $this->redirect("/users/login");
            return;
        }

        $messages = $this->Message->view_ajax($id, $page);
        $user_photo = $this->Message->get_user_photos($this->session->get("user_id"));
        $this->_render_header = false;
        $this->set("messages", $messages);
    }

}
